package estoquefacil

class ItemDeVendaController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [itemDeVendaInstanceList: ItemDeVenda.list(params), itemDeVendaInstanceTotal: ItemDeVenda.count()]
    }

    def create = {
        def itemDeVendaInstance = new ItemDeVenda()
        itemDeVendaInstance.properties = params
		def listaDeProdutos = null
		
		if (itemDeVendaInstance.pedido) {
			listaDeProdutos = Produto.findAllWhere(estoque:Pedido.get(itemDeVendaInstance.pedido.id).estoqueRemetente )
		}
        return [itemDeVendaInstance: itemDeVendaInstance, listaDeProdutos: listaDeProdutos]
    }

    def save = {
        def itemDeVendaInstance = new ItemDeVenda(params)
		def produtoInstance = Produto.get(itemDeVendaInstance.produto?.id)
		
		if (!itemDeVendaInstance.hasErrors()) {
			if (itemDeVendaInstance.quantidade > produtoInstance.quantidade) {
				flash.message = "Quantidade indicada nao existe no armazem"
			} else if (itemDeVendaInstance.save(flush: true)) {
				produtoInstance.quantidade -= itemDeVendaInstance.quantidade
				produtoInstance.save(flush: true)
				flash.message = "${message(code: 'default.created.message', args: [message(code: 'itemDeVenda.label', default: 'ItemDeVenda'), itemDeVendaInstance.id])}"
				
				if (itemDeVendaInstance.venda != null) {
					redirect(controller:'venda', action: "edit", id: itemDeVendaInstance.venda.id)
				} else if (itemDeVendaInstance.pedido != null) {
					redirect(controller:'pedido', action: "edit", id: itemDeVendaInstance.pedido.id)
				}
			}
		}
		
		render(view: "create", model: [itemDeVendaInstance: itemDeVendaInstance])
        
    }

    def show = {
        def itemDeVendaInstance = ItemDeVenda.get(params.id)
        if (!itemDeVendaInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'itemDeVenda.label', default: 'ItemDeVenda'), params.id])}"
            redirect(action: "list")
        }
        else {
            [itemDeVendaInstance: itemDeVendaInstance]
        }
    }

    def edit = {
        def itemDeVendaInstance = ItemDeVenda.get(params.id)
        if (!itemDeVendaInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'itemDeVenda.label', default: 'ItemDeVenda'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [itemDeVendaInstance: itemDeVendaInstance]
        }
    }

    def update = {
        def itemDeVendaInstance = ItemDeVenda.get(params.id)
        if (itemDeVendaInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (itemDeVendaInstance.version > version) {
                    
                    itemDeVendaInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'itemDeVenda.label', default: 'ItemDeVenda')] as Object[], "Another user has updated this ItemDeVenda while you were editing")
                    render(view: "edit", model: [itemDeVendaInstance: itemDeVendaInstance])
                    return
                }
            }
            itemDeVendaInstance.properties = params
            if (!itemDeVendaInstance.hasErrors() && itemDeVendaInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'itemDeVenda.label', default: 'ItemDeVenda'), itemDeVendaInstance.id])}"
                redirect(action: "show", id: itemDeVendaInstance.id)
            }
            else {
                render(view: "edit", model: [itemDeVendaInstance: itemDeVendaInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'itemDeVenda.label', default: 'ItemDeVenda'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def itemDeVendaInstance = ItemDeVenda.get(params.id)
        if (itemDeVendaInstance) {
            try {
                itemDeVendaInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'itemDeVenda.label', default: 'ItemDeVenda'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'itemDeVenda.label', default: 'ItemDeVenda'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'itemDeVenda.label', default: 'ItemDeVenda'), params.id])}"
            redirect(action: "list")
        }
    }
}
